﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Cinch;
using TeamTasker.Client.Presentation.Controls.SearchInTreeControl;
using TeamTasker.Contracts;

namespace TeamTasker.Client.Presentation.ViewModels
{
    public class TaskTreeViewModel : ViewModelBase
    {
        public TaskTreeViewModel()
        {
            IList<Task> tasks = new List<Task>();
            var task = new Task { Status = TaskStatus.NotStarted, Summary = "Team tasker project" };
            task.SubTasks.Add(new Task { Status = TaskStatus.NotStarted, Summary = "Iteration 1" });
            task.SubTasks[0].SubTasks.Add(new Task { Status = TaskStatus.NotStarted, Summary = "Implement 564fFJD" });
            task.SubTasks[0].SubTasks[0].SubTasks.Add(new Task { Status = TaskStatus.NotStarted, Summary = "Create tech design doc" });
            task.SubTasks[0].SubTasks[0].SubTasks.Add(new Task { Status = TaskStatus.NotStarted, Summary = "Tech design review" });
            task.SubTasks[0].SubTasks[0].SubTasks.Add(new Task { Status = TaskStatus.NotStarted, Summary = "Implement the FR" });
            task.SubTasks[0].SubTasks[0].SubTasks.Add(new Task { Status = TaskStatus.NotStarted, Summary = "FR code review" });
            task.SubTasks[0].SubTasks[0].SubTasks.Add(new Task { Status = TaskStatus.NotStarted, Summary = "FR QA acceptance testing" });
            task.SubTasks[0].SubTasks.Add(new Task
            {
                Summary = "Fix #538745",
                Status = TaskStatus.Completed,
                AssignedUser = new User { FullName = "Josh Smith" },
                Estimation = 16,
                ActualStartDate = new DateTime(2010, 04, 20),
                ActualEndDate = new DateTime(2010, 04, 27)
            });
            task.SubTasks[0].SubTasks.Add(new Task { Status = TaskStatus.NotStarted, Summary = "Fix #5867956" });
            task.SubTasks[0].SubTasks.Add(new Task { Status = TaskStatus.NotStarted, Summary = "Fix #45345" });
            task.SubTasks[0].SubTasks.Add(new Task { Status = TaskStatus.NotStarted, Summary = "Fix #784545" });
            task.SubTasks[0].SubTasks.Add(new Task { Status = TaskStatus.NotStarted, Summary = "Iteration acceptance testing" });
            task.SubTasks.Add(new Task { Status = TaskStatus.NotStarted, Summary = "Iteration 2" });
            task.SubTasks.Add(new Task { Status = TaskStatus.NotStarted, Summary = "Iteration 3" });
            tasks.Add(task);
            Nodes = new NodeModelHierarchicalCollection<Task>(tasks, t => t.SubTasks, CreateNodeFromTask);
            Mediator.Register(this);
            CreateNewTaskCommand = new SimpleCommand() { ExecuteDelegate = GoToAddEditTaskView };
            DeleteTaskCommand = new SimpleCommand() { ExecuteDelegate = DeleteTask }; 
        }

        public HierarchicalTaskListViewModel ParentViewModel { get; set; }

        public ICommand CreateNewTaskCommand { get; set; }

        public ICommand DeleteTaskCommand { get; set; }

        public INodeModelHierarchicalCollection Nodes { get; private set; }

        private void DeleteTask(object obj)
        {
            var taskToDelete = obj as Task;
            if (taskToDelete != null)
            {
                Nodes.FindNodeByData(obj);
            }
        }

        private void GoToAddEditTaskView(object obj)
        {
            if (ParentViewModel != null)
            {
                Mediator.NotifyColleagues("GoAddEditTaskView", ParentViewModel);
            }
        }

        private static NodeModel CreateNodeFromTask(Task task, int level)
        {
            return new NodeModel(task, task.Summary, level);
        }
    }
}
